<script>
    $(function () {
        $.fn.uploader = function (options) {
            var uploader = WebUploader.create({
                // swf文件路径
                swf: '${ctxStatic}/plugins/webuploader/Uploader.swf',
                // 文件接收服务端。
                server: options.url,
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '.picker',
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,
                thumb: {
                    width: 100,
                    height: 100,
                    type: 'image/jpg,jpeg,png',
                    allowMagnify: false
                },
                accept: {
                    title: 'Images',
                    extensions: 'jpg,jpeg,png',
                    mimeTypes: 'image/jpg,image/jpeg,image/png'
                },
                fileNumLimit: 5,
                fileSingleSizeLimit: 2048000,
                fileVal: "file",
                auto: false
            });

            // 当有文件被添加进队列的时候
            uploader.on( 'fileQueued', function( file ) {
                var li = '<div class="uploader-item">';
                li += '		<input name="upload-img-src" type="hidden" value=""/>';
                li += '		<img class="upload-img" src="" id="'+file.id+'"/>';
                li += '		<img class="del-img" src="${ctxStatic}/common/img/del.png" onClick="del(this)"/>'
                li += '   </div>';
                $('.uploader-list .picker').before(li);
                uploader.makeThumb( file, function( error, src ) {
                    $("#"+file.id).attr( 'src', src );
                });
            });

            // 上传成功
            uploader.on( 'uploadSuccess', function( file, response) {
                $("#"+file.id).prev().val(JSON.parse(response._raw).data)
            });

            // 上传失败
            uploader.on( 'uploadError', function( file ) {
                console.log("上传失败:"+file.id)
            });

            return uploader;
        }
    })

    //删除
    function del(_this){
        var id = $(_this).prev().attr("id");
        $(_this).parent().remove();
        uploader.removeFile(uploader.getFile(id));
    }

    //初始化图片显示
    function initShowImage(goodsImage) {
        var str = "";
        for(var i in goodsImage){
            var li = '<div class="uploader-item">';
            li += '		<input name="upload-img-src" type="hidden" value="'+goodsImage[i]+'"/>';
            li += '		<img class="upload-img" src="'+goodsImage[i]+'"/>';
            li += '		<img class="del-img" src="${ctxStatic}/common/img/del.png" onClick="del(this)"/>'
            li += '   </div>';
            str += li;
        }
        $('.uploader-list .picker').before(str);
    }
</script>